const CONTEXT = require('../../utils/public_var.js')
function getPromoteList(list) {

  let slicedItems = null
  // if (list.length == 2) {
  //   slicedItems = list.slice(list.length - 1, list.length);
  // } else if (list.length == 3) {
  //   slicedItems = list.slice(list.length - 2);
  // } else {
  //   slicedItems = list.slice(list.length - 3);
  // }
  slicedItems = list.slice(list.length - 1, list.length);
  console.log(slicedItems)

  return slicedItems
}
module.exports={
  ocrRequest(that,app,url,ocrTx){
    var con = that 
    if (ocrTx != "") {
      var message = {
        "role": "user",
        "content": ocrTx,
        "msgtype": CONTEXT.TEXT
      }
      var list = con.data.MessageList
      list.push(message)
      con.setData({
        MessageList: list,
      })
      con.setData({
        intoViewName: 'endview'
      })
      wx.showLoading({
        title: "主公请稍后，我在构思中"
      })
      wx.request({
        url: url,
        header: {
          'content-type': 'application/json',
        },
        method: 'POST',
        data: {
          "prompt": getPromoteList(con.data.MessageList),
          'openid': app.globalData.OpenID
        },
        success(res) {
          wx.hideLoading()
          console.log(res.data)
          if (res.data.statusCode == 200) {
            var sys_message = {
              "role": "system",
              "content": res.data.completeOpenAnswer.gptAnswer,
              "msgtype": CONTEXT.TEXT

            }
            list.push(sys_message)
            con.setData({
              MessageList: list,
              input_tx: "",
              totalCount: res.data.completeOpenAnswer.remainingGPTCount,
            })
            app.setCount(res.data.completeOpenAnswer.remainingGPTCount)

            app.saveMsg(that.data.bar_type,list)
          
            // child.initInputTx();

            con.setData({
              intoViewName: 'endview'
            })
          } else {

          }


        },
        fail(res) {
          wx.hideLoading()
          console.log(res)
        }
      })

    } else {
      wx.showToast({
        title: '请输入您的问题',
        icon: "none"
      })
    }
  },
  
  
}